Objectives:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from skimage.io import imread
import skimage.filters.rank as skr
from skimage.morphology import disk
ima = imread('http://homepages.ulb.ac.be/~odebeir/data/circles1.png')==0 #to be sure objects are = 1
plt.imshow(ima,cmap=plt.cm.gray);
# use the local maximum and local minimum for dilation and erosion
eroded_image = skr.minimum(ima,disk(5))
dilated_image = skr.maximum(ima,disk(5))
plt.figure(figsize=[10,8])
plt.subplot(1,2,1)
plt.imshow(eroded_image)
plt.title('erosion')
plt.subplot(1,2,2)
plt.imshow(dilated_image)
plt.title('dilation');
Starting from the example above:
by computing the remaining object surface after an opening with a structuring element of increasing size. Opening is a succession of one erosion folowed by one dilation using the same structuring element.
# -- Your code here -- #
from skimage.morphology import watershed,disk
import skimage.filters.rank as skr
from skimage.segmentation import mark_boundaries
rgba = imread('http://homepages.ulb.ac.be/~odebeir/data/road.png')
print("Image shape:",rgba.shape) # We can see that this image has 4 channels : R,G,B and alpha (transparency)
# The transparency channel is not actually used as everything is set to 255.
print("Min and max of alpha channel:",rgba[:,:,3].min(), rgba[:,:,3].max())
rgb = rgba[:,:,:3] # We only keep the first three channels
ima = rgb[:,:,0]
smoothed = skr.mean(ima,disk(4))
gradient = skr.gradient(smoothed,disk(1))
marks = np.zeros_like(ima)
marks[50,200] = 1
marks[250,200] = 2
marks[150,100] = 3
marks[150,400] = 4
ws = watershed(gradient,marks)
plt.figure(figsize=[8,8])
plt.subplot(2,2,1)
plt.imshow(ima,cmap=plt.cm.gray);
plt.subplot(2,2,2)
plt.imshow(gradient,cmap=plt.cm.gray);
plt.subplot(2,2,3)
plt.imshow(ws);
plt.subplot(2,2,4)
plt.imshow(mark_boundaries(rgb,ws));
('Image shape:', (299, 450, 4)) ('Min and max of alpha channel:', 255, 255)
Starting from the example above:
rgb = imread('peppers.jpg')
print(rgb.shape,rgb.min(),rgb.max())
plt.figure()
plt.imshow(rgb)
plt.show()
# -- Your code here -- #
((480, 640, 3), 0, 255)
from scipy import ndimage as ndi
ima = imread('http://homepages.ulb.ac.be/~odebeir/data/circles1.png')==0 #to be sure objects are = 1
# compute the euclidian distance to the background
distance = ndi.distance_transform_edt(ima)
plt.figure(figsize=[8,8])
plt.imshow(distance)
plt.colorbar();
m,n = ima.shape
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure(figsize=[10,10])
ax = fig.gca(projection='3d')
X,Y = np.meshgrid(range(n),range(m))
Z = distance
ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3)
plt.xlabel('$x$')
plt.ylabel('$y$')
plt.title('$distance$');
# -- Your code here -- #